package com.web.portal.controller;

import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.apache.log4j.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.web.portal.entity.Faculty;
import com.web.portal.exception.PortalException;
import com.web.portal.pojo.FacultyPojo;
import com.web.portal.service.FacultyService;

@Controller
public class CrudController {
	private static final Logger LOG = Logger.getLogger(CrudController.class);
	@Autowired
	FacultyService service;

	@RequestMapping(value = "/admin/viewfaculty.do", method = RequestMethod.GET)
	public String viewFacultyOrStaff(@RequestParam("email") final String email,
			Map<String, Object> map) throws PortalException {
		if (!(email == null)) {
			List<Faculty> thisFaculty = new ArrayList<Faculty>();
			Faculty faculty = service.getFacultyImageOrMapFile(email);
			thisFaculty.add(faculty);
			map.put("faculty", thisFaculty);
			return "faculty_view";
		}
		return null;
	}

	@RequestMapping(value = "/admin/delete.do", method = RequestMethod.GET)
	public String editFacultyOrStaff(@RequestParam("email") final String email,
			Map<String, Object> map) throws PortalException {
		LOG.debug("Requesting for delete.................................");
		if (!(email == null)) {
			List<Faculty> thisFaculty = new ArrayList<Faculty>();
			Faculty faculty = service.getFacultyImageOrMapFile(email);
			/*
			 * delete faculty
			 */
			service.delete(faculty);

			return "redirect:/admin/manager.do";
		}
		return null;
	}

	@SuppressWarnings("null")
	@RequestMapping(value = "/admin/getedit.do", method = RequestMethod.GET)
	public String getEditPage(@RequestParam("id") final String id,
			HttpServletRequest request, Map model) throws PortalException {
		LOG.debug("Getting edit page for the record................");
		if (!(id == null)) {
			Faculty faculty = service.getFaculty(id);
			request.getSession().setAttribute("id", id);

			FacultyPojo facultyPojo = new FacultyPojo();
			facultyPojo.setId(faculty.getId());
			facultyPojo.setFirstName(faculty.getFirstName());
			facultyPojo.setLastName(faculty.getLastName());
			/*
			 * implement later so byte can be coverted into a file and saved in
			 * temporary file and later can be accessed using <img tag. Using
			 * fileOutputstream will do the trick
			 */
			facultyPojo.setMyFileMap(null);
			facultyPojo.setMyFileMap(null);
			facultyPojo.setDescription(faculty.getDescription());
			facultyPojo.setMail(faculty.getEmail());
			facultyPojo.setPhone(faculty.getPhone());
			facultyPojo.setResearch(faculty.getResearch());
			facultyPojo.setPosition(faculty.getPosition());
			model.put("facultyPojo", facultyPojo);
			return "faculty_edit";
		}
		return null;

	}

	@RequestMapping(value = "/admin/getedited.do", method = RequestMethod.POST)
	public String editFaculty(
			@Valid @ModelAttribute("facultyPojo") final FacultyPojo facultyPojo,
			Map model, HttpServletRequest request) {
		/*
		 * update
		 */
		LOG.debug("Updating the page.............");

		/*
		 * if (facultyPojo.getId() == null) { Faculty faculty = new Faculty();
		 * faculty.setFirstName(facultyPojo.getFirstName());
		 * faculty.setLastName(facultyPojo.getLastName());
		 * faculty.setImage(facultyPojo.getMyFileImage().getBytes()); byte[] map
		 * = facultyPojo.getMyFileMap().getBytes(); faculty.setMap(map);
		 * faculty.setDescription(facultyPojo.getDescription());
		 * faculty.setEmail(facultyPojo.getMail());
		 * faculty.setResearch(facultyPojo.getResearch());
		 * faculty.setPhone(facultyPojo.getPhone());
		 * faculty.setPosition(facultyPojo.getPosition());
		 * 
		 * service.addFaculty(faculty); return "redirect:/manager.do"; } else {
		 */
		String id = (String) request.getSession().getAttribute("id");
		Faculty faculty = service.getFaculty(id);
		faculty.setFirstName(facultyPojo.getFirstName());
		faculty.setLastName(facultyPojo.getLastName());
		if (facultyPojo.getMyFileImage() == null) {
			faculty.setImage(null);
		} else {
			faculty.setImage(facultyPojo.getMyFileImage().getBytes());
		}
		if (facultyPojo.getMyFileMap() == null) {
			faculty.setMap(null);
		} else {
			faculty.setMap(facultyPojo.getMyFileMap().getBytes());
		}

		byte[] map = facultyPojo.getMyFileMap().getBytes();
		faculty.setMap(map);
		faculty.setDescription(facultyPojo.getDescription());
		faculty.setEmail(facultyPojo.getMail());
		faculty.setResearch(facultyPojo.getResearch());
		faculty.setPhone(facultyPojo.getPhone());
		faculty.setPosition(facultyPojo.getPosition());
		service.createOrUpdate(faculty);
		LOG.debug("Updated successfully............................");

		return "redirect:/admin/manager.do";

	}
}
